package com.huan.yygh.order.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huan.yygh.common.exception.YyghException;
import com.huan.yygh.common.helper.HttpRequestHelper;
import com.huan.yygh.common.result.ResultCodeEnum;
import com.huan.yygh.enums.OrderStatusEnum;
import com.huan.yygh.enums.PaymentStatusEnum;
import com.huan.yygh.hosp.HospFeignClient;
import com.huan.yygh.model.order.OrderInfo;
import com.huan.yygh.model.order.PaymentInfo;
import com.huan.yygh.order.mapper.PaymentMapper;
import com.huan.yygh.order.service.OrderService;
import com.huan.yygh.order.service.PaymentService;
import com.huan.yygh.vo.order.SignInfoVo;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class PaymentServiceImpl extends ServiceImpl<PaymentMapper, PaymentInfo> implements PaymentService {
    @Autowired
    private OrderService orderService;
    @Autowired
    private HospFeignClient hospFeignClient;
    @Override
    public void savePaymentInfo(OrderInfo orderInfo, Integer paymentType) {
        QueryWrapper<PaymentInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_id", orderInfo.getId());
        queryWrapper.eq("payment_type", paymentType);
        Integer count = baseMapper.selectCount(queryWrapper);
        if(count >0) return;
        // 保存交易记录
        PaymentInfo paymentInfo = new PaymentInfo();
        paymentInfo.setCreateTime(new Date());
        paymentInfo.setOrderId(orderInfo.getId());
        paymentInfo.setPaymentType(paymentType);
        paymentInfo.setOutTradeNo(orderInfo.getOutTradeNo());
        paymentInfo.setPaymentStatus(PaymentStatusEnum.UNPAID.getStatus());
        String subject = new DateTime(orderInfo.getReserveDate()).toString("yyyy-MM-dd")+"|"+orderInfo.getHosname()+"|"+orderInfo.getDepname()+"|"+orderInfo.getTitle();
        paymentInfo.setSubject(subject);
        paymentInfo.setTotalAmount(orderInfo.getAmount());
        baseMapper.insert(paymentInfo);
    }

    @Override
    public void paySuccess(String outTradeNo, Integer paymentType, Map<String, String> paramMap) {
            PaymentInfo paymentInfo = this.getPaymentInfo(outTradeNo, paymentType);
            if (null == paymentInfo) {
                throw new YyghException(ResultCodeEnum.PARAM_ERROR);
            }
            if (paymentInfo.getPaymentStatus() != PaymentStatusEnum.UNPAID.getStatus()) {
                return;
            }
            //修改支付状态
            PaymentInfo paymentInfoUpd = new PaymentInfo();
            paymentInfoUpd.setPaymentStatus(PaymentStatusEnum.PAID.getStatus());
            paymentInfoUpd.setTradeNo(paramMap.get("transaction_id"));
            paymentInfoUpd.setCallbackTime(new Date());
            paymentInfoUpd.setCallbackContent(paramMap.toString());
            this.updatePaymentInfo(outTradeNo, paymentInfoUpd);
            //修改订单状态
            OrderInfo orderInfo = orderService.getById(paymentInfo.getOrderId());
            orderInfo.setOrderStatus(OrderStatusEnum.PAID.getStatus());
            orderService.updateById(orderInfo);
            // 调用医院接口，通知更新支付状态

            SignInfoVo signInfoVo
                    = hospFeignClient.getSignInfoVo(orderInfo.getHoscode());
            if(null == signInfoVo) {
                throw new YyghException(ResultCodeEnum.PARAM_ERROR);
            }
            Map<String, Object> reqMap = new HashMap<>();
            reqMap.put("hoscode",orderInfo.getHoscode());
            reqMap.put("hosRecordId",orderInfo.getHosRecordId());
            reqMap.put("timestamp", HttpRequestHelper.getTimestamp());
            String sign = HttpRequestHelper.getSign(reqMap, signInfoVo.getSignKey());
            reqMap.put("sign", sign);
            JSONObject result = HttpRequestHelper.sendRequest(reqMap, signInfoVo.getApiUrl()+"/order/updatePayStatus");
            if(result.getInteger("code") != 200) {
                throw new YyghException(result.getString("message"), ResultCodeEnum.FAIL.getCode());
            }

    }

    @Override
    public PaymentInfo getPaymentInfo(Long orderId, Integer paymentType) {
        QueryWrapper<PaymentInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_id", orderId);
        queryWrapper.eq("payment_type", paymentType);
        return baseMapper.selectOne(queryWrapper);
    }

    /**
     * 获取支付记录
     */
    private PaymentInfo getPaymentInfo(String outTradeNo, Integer paymentType) {
        QueryWrapper<PaymentInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("out_trade_no", outTradeNo);
        queryWrapper.eq("payment_type", paymentType);
        return baseMapper.selectOne(queryWrapper);
    }
    /**
     * 更改支付记录
     */
    private void updatePaymentInfo(String outTradeNo, PaymentInfo paymentInfoUpd) {
        QueryWrapper<PaymentInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("out_trade_no", outTradeNo);
        baseMapper.update(paymentInfoUpd, queryWrapper);
    }
}
